package com.example.uiapp;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.example.uiapp.databinding.ActivityMainBinding;
import com.google.android.material.navigation.NavigationBarView;
import com.google.android.material.navigation.NavigationView;
import com.google.android.material.snackbar.Snackbar;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;
    private NavigationBarView.OnItemSelectedListener navListener = new NavigationBarView.OnItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            binding.contentMain.toolbar.setTitle(item.getTitle());
            switch (item.getItemId()) {
                case R.id.navigation_home:
//                    binding.tvMessage.setText(R.string.title_home);
                    binding.contentMain.viewPager.setCurrentItem(0);
                    return true;
                case R.id.navigation_dashboard:
//                    binding.tvMessage.setText(R.string.title_dashboard);
                    binding.contentMain.viewPager.setCurrentItem(1);
                    return true;
                case R.id.navigation_notification:
//                    binding.tvMessage.setText(R.string.title_notification);
                    binding.contentMain.viewPager.setCurrentItem(2);
                    return true;
            }
            return false;
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        // 设置自定义Toolbar
        setSupportActionBar(binding.contentMain.toolbar); // 界面主题要设为NoActionBar

        // 配置ViewPager的适配器
        final MainViewPageAdapter viewPageAdapter = new MainViewPageAdapter(this);
        binding.contentMain.viewPager.setAdapter(viewPageAdapter);
        binding.contentMain.viewPager.setOffscreenPageLimit(viewPageAdapter.getItemCount() - 1);
        // 禁止ViewPager滑动
//        binding.viewPager.setUserInputEnabled(false);
        // 注册ViewPager的滑动事件监听
        binding.contentMain.viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                MenuItem menuItem = binding.contentMain.navigation.getMenu().getItem(position);
                // 设置导航栏被选中
                menuItem.setChecked(true);
                // 设置自定义标题栏的名称
                binding.contentMain.toolbar.setTitle(menuItem.getTitle());
            }
        });
        // 设置ViewPager的转场动画
//        binding.viewPager.setPageTransformer(new ZoomOutPageTransformer());

        // 设置底部导航的监听器
        binding.contentMain.navigation.setOnItemSelectedListener(navListener);
        bindNavigationDrawer();
    }
    public void onFABClick(View view) {
        showSnackbarMsg("FAB button click");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        final int id = item.getItemId();
        if (id == R.id.action_settings) {
            showSnackbarMsg(item.getTitle().toString());
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void bindNavigationDrawer() {
        final ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,
                binding.drawerLayout,
                binding.contentMain.toolbar,
                R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        binding.drawerLayout.addDrawerListener(toggle);
        toggle.syncState();

        binding.navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                final int id = item.getItemId();
                if (id == R.id.nav_home) {
                    showSnackbarMsg("左侧导航菜单-" + item.getTitle() + " click");
                } else if(id == R.id.nav_gallery) {
                    showSnackbarMsg("左侧导航菜单-相册 click");
                } else if(id == R.id.nav_slideshow) {
                    showSnackbarMsg("左侧导航菜单-幻灯 click");
                }
                binding.drawerLayout.closeDrawer(GravityCompat.START);
                return true;
            }
        });
    }

    private void showSnackbarMsg(String message) {
        Snackbar snackbar = Snackbar.make(binding.contentMain.coordinatorLayout, message, Snackbar.LENGTH_LONG);
        snackbar.setAction("ok", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                snackbar.dismiss();
            }
        });
        snackbar.show();
    }

    private static class MainViewPageAdapter extends FragmentStateAdapter {
        public MainViewPageAdapter(@NonNull FragmentActivity fragmentActivity) {
            super(fragmentActivity);
        }
        @NonNull
        @Override
        public Fragment createFragment(int position) {
           switch(position) {
               case 0:
                   return HomeFragment.newInstance();
               case 1:
                   return DashboardFragment.newInstance();
               case 2:
                   return NotificationFragment.newInstance();
           }
           return HomeFragment.newInstance();
        }
        @Override
        public int getItemCount() {
            return 3;
        }
    }

    private static class ZoomOutPageTransformer implements ViewPager2.PageTransformer {
        private static final float MIN_SCALE = 0.85f;
        private static final float MIN_ALPHA = 0.5f;
        @Override
        public void transformPage(@NonNull View view, float position) {
            int pageWidth = view.getWidth();
            int pageHeight = view.getHeight();

            if (position < -1) { // [-Infinity,-1)
                // This page is way off-screen to the left.
                view.setAlpha(0f);

            } else if (position <= 1) { // [-1,1]
                // Modify the default slide transition to shrink the page as well
                float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
                float vertMargin = pageHeight * (1 - scaleFactor) / 2;
                float horzMargin = pageWidth * (1 - scaleFactor) / 2;
                if (position < 0) {
                    view.setTranslationX(horzMargin - vertMargin / 2);
                } else {
                    view.setTranslationX(-horzMargin + vertMargin / 2);
                }

                // Scale the page down (between MIN_SCALE and 1)
                view.setScaleX(scaleFactor);
                view.setScaleY(scaleFactor);

                // Fade the page relative to its size.
                view.setAlpha(MIN_ALPHA +
                        (scaleFactor - MIN_SCALE) /
                                (1 - MIN_SCALE) * (1 - MIN_ALPHA));

            } else { // (1,+Infinity]
                // This page is way off-screen to the right.
                view.setAlpha(0f);
            }
        }
    }
}